package com.fisionlabs.driver;

import java.io.IOException;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;

public class Driver 
{
	NetworkInterface[] devices;
	JpcapCaptor jpcapCaptor;
	
	public Driver()
	{
		this.devices = JpcapCaptor.getDeviceList();
	}

	public Driver( int device, int snaplen, boolean promisc, int to_ms )
	{
		this.devices = JpcapCaptor.getDeviceList();
		try {
			this.jpcapCaptor = JpcapCaptor.openDevice(this.devices[device], snaplen, promisc, to_ms);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean initialize( int device, int snaplen, boolean promisc, int to_ms ){
		try {
			this.jpcapCaptor = JpcapCaptor.openDevice(this.devices[device], snaplen, promisc, to_ms);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public String showInterface()
	{
		String str = "";
		
		for (int i = 0; i < devices.length; i++) {
			str += i+" :"+devices[i].name + "(" + devices[i].description+")";
			str += "    data link:"+devices[i].datalink_name + "(" + devices[i].datalink_description+")";
			str += "    MAC address:";
			for (byte b : devices[i].mac_address)
				str += Integer.toHexString(b&0xff) + ":";
			str += "\n";
			for (NetworkInterfaceAddress a : devices[i].addresses)
				str += "    address:"+a.address + " " + a.subnet + " " + a.broadcast;
		}
		
		return str;
	}
	
	public NetworkInterface[] getDevices() {
		return devices;
	}

	public void setDevices(NetworkInterface[] devices) {
		this.devices = devices;
	}

	public JpcapCaptor getJpcapCaptor() {
		return jpcapCaptor;
	}

	public void setJpcapCaptor(JpcapCaptor jpcapCaptor) {
		this.jpcapCaptor = jpcapCaptor;
	}
	
}
